<template>
	<div>
		<navigation-bar>
			<div class="searchContent">
				<el-input v-model="frigName" placeholder="请输入冰箱名称" size="mini"
					style="width: 150px; margin-right: 10px"></el-input>
				<el-select clearable v-model="deviceStatus" size="small" placeholder="请选择冰箱状态"
					style="width: 150px; margin-right: 10px">
					<el-option :label="select.label" :value="select.value" v-for="(select, sIndex) in deviceStatusList"
						:key="sIndex"></el-option>
				</el-select>
				<el-select clearable v-model="frigType" size="small" placeholder="请选择冰箱类型"
					style="width: 150px; margin-right: 10px">
					<el-option :label="select.label" :value="select.value" v-for="(select, sIndex) in frigTypeList"
						:key="sIndex"></el-option>
				</el-select>

				<custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
				<custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
				<!-- <custom-button
					type="add"
					style="margin-right: 10px"
					@addClick="addClick"
				></custom-button> -->
				<custom-button type="print" title="子标签打印"></custom-button>
			</div>
		</navigation-bar>
		<table-index :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" :pageSize="pageSize"
			:pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @editRow="editRow" @viewRow="viewRow"
			@deleteRow="deleteRow"></table-index>

		<div v-if="dialogShow">
			<custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
				<form-index :formConfig="formConfig" @submit="submit" :type="type" @formInputChange="formInputChange"
					ref="form">
					<!--  <div>这里还可以插入其它内容进行拓展</div> -->
				</form-index>
			</custom-dialog>
		</div>
	</div>
</template>

<script>
import { sampleRefrigeratorForm } from "@/assets/form/managementCenter/sampleManagement/sampleRefrigeratorForm"
import customDialog from "@/components/customComponents/customDialog"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import { AjaxCommon } from "@/libs/apiCommon"
import { CardId } from "@/libs/cardId"
import { PopupCommon } from "@/libs/popupCommon"
import { SessionCommon } from "@/libs/sessionCommon"
import { Utility } from "@/libs/utility"
export default {
	components: {
		navigationBar,
		customButton,
		tableIndex,
		customDialog,
		formIndex,
	},
	data() {
		return {
			frigType: "",
			// 冰箱类型
			frigTypeList: Utility.getDictList("frig_type"),
			// 冰箱名称
			frigName: "",
			// 冰箱状态
			deviceStatus: "",
			deviceStatusList: Utility.getDictList("status"),
			// table 相关参数  begin
			dataSource: [],
			tableColumns: [
				{
					prop: "frigName",
					label: "冰箱名称",
				},
				{
					prop: "frigCode",
					label: "冰箱编码",
					width: 120,
				},
				{
					prop: "frigModel",
					label: "冰箱型号",
				},
				{
					prop: "temperature",
					label: "存放温度(℃)",
					width: 140,
				},
				{
					prop: "manufacturer",
					label: "生产厂家",
					width: 140,
				},
				{
					prop: "productionDate",
					label: "出厂日期",
					width: 140,
				},
				// {
				// 	prop: "frigType",
				// 	label: "冰箱类型",
				// 	width: 120,
				// 	dictName: "frig_type",
				// },
				{
					prop: "factoryNo",
					label: "出厂编号",
					width: 120,
				},
				{
					prop: "purchaseDate",
					label: "购买日期",
				},
				{
					prop: "startDate",
					label: "启动日期",
				},
				{
					prop: "technicalPram",
					label: "主要技术参数",
				},
				{
					prop: "deviceStatus",
					label: "设备状态",
					dictName: "status",
				},
				{
					prop: "rowsNum",
					label: "几行",
				},
				{
					prop: "columnNum",
					label: "几列",
				},
				{
					prop: "chargeByName",
					label: "负责人",
				},
				{
					prop: "verificationDate",
					label: "检定日期",
				},
				{
					prop: "deptName",
					label: "所属部门",
				},
				{
					prop: "storePosition",
					label: "存放位置",
				},
				{
					prop: "remarks",
					label: "备注",
				},
			],
			btnList: ["edit", "view", "delete"],
			pageSize: 20,
			pageCount: 100,
			pageNo: 1,
			// table 相关参数  end

			// 弹窗是否显示
			dialogShow: false,
			// 弹窗标题 (新增修改不同)
			dialogTitle: "",

			// form的配置信息
			formConfig: sampleRefrigeratorForm,
			type: "add",
		}
	},

	methods: {
		formInputChange(e) {
			if (e.filedName == "idCard") {
				const data = CardId.getInfoByCardId(e.value)
				if (data) {
					let newData = e.allData
					newData["birthday"] = data.birthday
					newData["sex"] = data.sex
					this.$refs.form.changeFormValue(newData)
				}
			}
		},
		submit(d) {
			let self = this
			console.log(d)
			if (d.id) {
				AjaxCommon("/ccdc/pm/sample/sampleFrig/edit", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							self.initPage()
							self.dialogClose()
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			} else {
				delete d.id
				AjaxCommon("/ccdc/pm/sample/sampleFrig/add", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							self.initPage()
							self.dialogClose()
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			}
		},
		dialogClose() {
			this.dialogShow = false
		},
		searchClick() {
			this.pageNo = 1
			this.initPage()
		},
		resetClick() {
			this.frigName = ""
			this.deviceStatus = ""
			this.frigType = ""
		},
		addClick() {
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						t.filedValue = ""
					}
				})
			})
			this.formConfig = formConfigItem
			this.type = "add"
			this.dialogShow = true
			this.dialogTitle =
				"新增" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		// 分页点击事件
		pageChange(p) {
			this.pageNo = p
			this.initPage()
		},
		editRow(d) {
			this.type = "edit"
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						t.filedValue = d.data[t[res]]
					}
					if (res.fieldName == 'deviceId') {
						var item = this.dataSource.find(c => c.id = d.id);
						t.filedValue = item.deviceId;
					}
				})
			})
			this.formConfig = formConfigItem
			this.dialogShow = true
			this.dialogTitle =
				"编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		viewRow(d) {
			this.type = "look"
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						t.filedValue = d.data[t[res]]
					}
				})
			})
			this.formConfig = formConfigItem
			this.dialogShow = true
			this.dialogTitle =
				"查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		deleteRow(d) {
			const id = d.data.id
			let self = this
			AjaxCommon("/ccdc/pm/sample/sampleFrig/delete?id=" + id, "delete", { optReason: d.optReason }).then(
				(res) => {
					console.log(res, "res...............")
					if (res.data.code == 200) {
						PopupCommon.notify("success", "删除成功")
						self.initPage()
						self.dialogClose()
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				}
			)
		},
		// 初始化事件
		initPage() {
			let self = this
			AjaxCommon("/ccdc/pm/sample/sampleFrig/listByPage", "post", {
				frigType: this.frigType,
				frigName: this.frigName,
				deviceStatus: this.deviceStatus,
				pageNo: this.pageNo,
				pageSize: this.pageSize,
			}).then((res) => {
				if (res.data.code == 200) {
					console.log(res.data)
					self.dataSource = res.data.result.records
					self.pageCount = res.data.result.total
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
		},
	},
	mounted() {
		this.initPage()
	},
}
</script>

<style></style>
